package com.yingjie.commonbase.commons;

import java.io.Serializable;

/**
 *统一Json返回值封装对象
 * 实际项目中需要参考接口文档的规范定义这个类
 */
public class ReturnObject<T> implements Serializable {
    //响应的状态码，0 表示成功  1表示失败 ...其他值表示其他失败情况
    private int code;
    //响应信息，例如请求成功或请求失败
    private String msg;
    //响应的具体数据（业务响应数据）
    private T result;

   public static ReturnObject  SUCCESS(){
       return new ReturnObject(1,"请求成功",null);
   }
    public static ReturnObject  SUCCESS(Object result){
        return new ReturnObject(1,"请求成功",result);
    }
    public static ReturnObject  SUCCESS(String msg,Object result){
        return new ReturnObject(1,"请求成功",result);
    }

    public static ReturnObject ERROR(){
        return new ReturnObject(0,"请求失败",null);
    }
    public static ReturnObject ERROR(String msg){
        return new ReturnObject(0,msg,null);
    }
    public static ReturnObject ERROR(int code,String msg){
        return new ReturnObject(code,msg,null);
    }
    public static ReturnObject ERROR(int code,String msg,Object result){
        return new ReturnObject(code,msg,result);
    }
    public static ReturnObject ERROR(ReturnCodeMsg returnCodeMsg){
        return new ReturnObject(returnCodeMsg.value(), returnCodeMsg.msg(),null);
    }
    public static ReturnObject ERROR(ReturnCodeMsg returnCodeMsg, Object result){
        return new ReturnObject(returnCodeMsg.value(), returnCodeMsg.msg(),result);
    }

    public ReturnObject() {
    }

    public ReturnObject(int code, String msg, T result) {
        this.code = code;
        this.msg = msg;
        this.result = result;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getResult() {
        return result;
    }

    public void setResult(T result) {
        this.result = result;
    }
}
